package com.tsd.jxc.stock.service.impl;

import com.tsd.common.vo.BatchDeleteLogicVo;
import com.tsd.core.thread.ThreadPoolTaskUtil;
import com.tsd.core.utils.DoubleUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.stock.dao.JxcStockTransferItemMapper;
import com.tsd.jxc.stock.entity.JxcStockTransferExt;
import com.tsd.jxc.stock.entity.JxcStockTransferItem;
import com.tsd.jxc.stock.entity.JxcStockTransferItemExt;
import com.tsd.jxc.stock.service.JxcStockTransferItemService;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcStockTransferItem 服务实现层
 *
 * @author Hillpool
 * @date 2022/12/12 11:32:10
 */
@Service
public class JxcStockTransferItemServiceImpl extends BaseServiceImpl implements JxcStockTransferItemService {

    @Resource
    private JxcStockTransferItemMapper jxcStockTransferItemMapper;

    @Override
    public JxcStockTransferItem getJxcStockTransferItemById(Long id) throws Exception {
        return jxcStockTransferItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public JxcStockTransferItemExt queryBySid(String sid) throws Exception {
        return jxcStockTransferItemMapper.selectBySid(sid);
    }

    @Override
    public List<JxcStockTransferItemExt> queryValidByTransferList(List<String> transfer_list) {
        List<JxcStockTransferItemExt> itemExtList = ThreadPoolTaskUtil.queryInList4Split(transfer_list, list -> {
            JxcStockTransferItemExt params = new JxcStockTransferItemExt();
            params.setDr(0);
            params.setTransfer_list(list);
            return jxcStockTransferItemMapper.selectByEntity(params);
        });
        return itemExtList;
    }

    @Override
    public void deleteJxcStockTransferItems(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            JxcStockTransferItem old = jxcStockTransferItemMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            jxcStockTransferItemMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveJxcStockTransferItem(JxcStockTransferItemExt record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            jxcStockTransferItemMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            jxcStockTransferItemMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void saveJxcStockTransferItem(JxcStockTransferExt record, SysUser opUser) throws Exception {
        List<JxcStockTransferItemExt> itemList = record.getItem_list();
        super.checkEmptyList(itemList, "单据明细不能为空");
        List<Long> existIdList = ListUtil.filterMap(itemList, t -> !HlpUtils.isEmpty(t.getId()), JxcStockTransferItemExt::getId);
        JxcStockTransferItemExt params = new JxcStockTransferItemExt();
        params.setDr(0);
        params.setTransfer_sid(record.getSid());
        List<JxcStockTransferItemExt> oldList = jxcStockTransferItemMapper.selectByEntity(params);

        //删除不存在订单明细
        List<JxcStockTransferItemExt> delList = ListUtil.filter(oldList, t -> !existIdList.contains(t.getId()));
        if (!HlpUtils.isEmptyList(delList)) {
            List<Long> delIds = ListUtil.map(delList, JxcStockTransferItemExt::getId);
            BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo(opUser);
            logicVo.setIdList(delIds);
            jxcStockTransferItemMapper.deleteLogicByIdList(logicVo);

            List<String> delSids = ListUtil.map(delList, JxcStockTransferItemExt::getSid);
            logicVo = new BatchDeleteLogicVo(opUser);
            logicVo.setSidList(delSids);
            jxcStockTransferItemMapper.deleteLogicByItemList(logicVo);
        }

        double totalQty = 0D;
        for (JxcStockTransferItemExt itemExt : itemList) {
            super.checkEmpty(itemExt.getQty(), "第" + itemExt.getSn() + "项明细数量不能为空");
            super.checkEmpty(itemExt.getPrice(), "第" + itemExt.getSn() + "项明细价格不能为空");
            itemExt.setTransfer_sid(record.getSid());
            if (HlpUtils.isEmpty(itemExt.getAmount())) {
                double amount = DoubleUtil.mul(itemExt.getQty(), itemExt.getPrice());
                itemExt.setAmount(BigDecimal.valueOf(amount));
            }
            totalQty = DoubleUtil.add(totalQty, itemExt.getQty());
            this.saveJxcStockTransferItem(itemExt, opUser);
        }
    }

    @Override
    public List<JxcStockTransferItemExt> findJxcStockTransferItems(Map<String, Object> params, PageBean page) throws Exception {
        return jxcStockTransferItemMapper.selectByPage(params, page);
    }

    @Override
    public List<JxcStockTransferItemExt> findJxcStockTransferItems(Map<String, Object> params) throws Exception {
        return jxcStockTransferItemMapper.selectByParams(params);
    }

    @Override
    public List<JxcStockTransferItemExt> queryValidByOrderList(List<String> order_list) {
        return ThreadPoolTaskUtil.queryInList4Split(order_list, list -> {
            JxcStockTransferItemExt params = new JxcStockTransferItemExt();
            params.setDr(0);
            params.setTransfer_list(list);
            return jxcStockTransferItemMapper.selectByEntity(params);
        });
    }
}